package com.wei.tang.widget;

import com.wei.tang.R;
import com.wei.tang.util.TDevice;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class EmptyLayout extends LinearLayout implements android.view.View.OnClickListener {

	/**
	 * 没有网的情况
	 */
	public static final int NONET = 1;
	/**
	 * 加载的情况
	 */
	public static final int LOADING = 2;
	/**
	 * 没有数据的情况
	 */
	public static final int NODATA = 3;
	/**
	 * 隐藏
	 */
	public static final int HIDE = 4;
	/**
	 * 没有数据点击加载数据
	 */
	public static final int NODATA_CLICK = 5;
	/**
	 * 没有登录
	 */
	public static final int NOLOGIN = 6;

	private boolean clickEnable = true;
	private final Context context;
	private android.view.View.OnClickListener listener;
	private int mErrorState;
	private ProgressBar animProgress;
	private TextView tv;
	public ImageView img;
	private String strException = "";
	private String strNoDataContent = "";

	public EmptyLayout(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public EmptyLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	private void init() {
		View view = View.inflate(context, R.layout.view_error_layout, null);
		img = (ImageView) view.findViewById(R.id.img_error_layout);
		tv = (TextView) view.findViewById(R.id.tv_error_layout);
		animProgress = (ProgressBar) view.findViewById(R.id.animProgress);

		setBackgroundColor(-1);
		setOnClickListener(this);
		img.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (clickEnable) {
					if (listener != null)
						listener.onClick(v);
				}
			}
		});
		addView(view);
	}

	public void dismiss() {
		mErrorState = HIDE;
		setVisibility(View.GONE);
	}

	public int getErrorState() {
		return mErrorState;
	}

	public boolean isLoadError() {
		return mErrorState == NONET;
	}

	public boolean isLoading() {
		return mErrorState == LOADING;
	}

	@Override
	public void onClick(View v) {
		if (clickEnable) {
			if (listener != null)
				listener.onClick(v);
		}
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
	}

	public void setErrorMessage(String msg) {
		tv.setText(msg);
	}

	public void setErrorImag(int imgResource) {
		try {
			img.setImageResource(imgResource);
		} catch (Exception e) {
		}
	}

	public void setException(int i, String str) {
		setErrorType(i);
		this.strException = str;
	}

	public void setErrorType(int i) {
		setVisibility(View.VISIBLE);
		switch (i) {
		case NONET:
			mErrorState = NONET;
			if (TDevice.hasInternet()) {
				tv.setText(strException);
				img.setBackgroundResource(R.drawable.pagefailed_bg);
			} else {
				tv.setText(R.string.error_view_network_error_click_to_refresh);
				img.setBackgroundResource(R.drawable.page_icon_network);
			}
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			clickEnable = true;
			break;
		case LOADING:
			mErrorState = LOADING;
			animProgress.setVisibility(View.VISIBLE);
			img.setVisibility(View.GONE);
			tv.setText(R.string.loading);
			clickEnable = false;
			break;
		case NODATA:
			mErrorState = NODATA;
			img.setVisibility(View.GONE);
			animProgress.setVisibility(View.GONE);
			tv.setText("暂时无数据哦～");
			clickEnable = false;
			break;
		case HIDE:
			mErrorState = HIDE;
			setVisibility(View.GONE);
			break;
		case NODATA_CLICK:
			mErrorState = NODATA_CLICK;
			img.setBackgroundResource(R.drawable.page_icon_empty);
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			tv.setText("网络异常，请稍后再试！");
			clickEnable = true;
			break;
		case NOLOGIN:
			mErrorState = NOLOGIN;
			tv.setText(R.string.no_login_click_to_login);
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			clickEnable = true;
			break;
		}
	}

	public void setNoDataContent(String noDataContent) {
		strNoDataContent = noDataContent;
	}

	public void setOnLayoutClickListener(View.OnClickListener listener) {
		this.listener = listener;
	}

	public void setTvNoDataContent() {
		if (!strNoDataContent.equals(""))
			tv.setText(strNoDataContent);
		else
			tv.setText(R.string.error_view_no_data);
	}

	@Override
	public void setVisibility(int visibility) {
		if (visibility == View.GONE)
			mErrorState = HIDE;
		super.setVisibility(visibility);
	}
}
